package com.woniuxy.house.mapper;

import com.woniuxy.house.entity.Source;
import com.woniuxy.house.entity.User;
import com.woniuxy.house.provider.UserSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 操作用户表接口
 * @author baobo
 * @date 2022-08-27-10:21
 */
public interface UserMapper {
	/**
	 * 新增用户
	 * @param user	用户实体类
	 */
	@Insert("insert into house_user(ACCOUNT,other_account,PASSWORD,head_photo,telephone,regist_time,user_sign,STATUS)" +
			" values(#{account},#{otherAccount},#{password},#{headPhoto},#{telephone},#{registTime},#{userSign},#{status})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	void add(User user);

	/**
	 * 通过用户id逻辑删除用户
	 * 默认正常状态1标识
	 * 删除状态使用字符2标识
	 * @param id	用户id
	 */
	@Update("update  house_user set status='2' where id=#{id}")
	void deleteById(Integer id);

	/**
	 * 根据用户id,属性是否为空更新用户字段
	 * @param user	用户实体类
	 */
	@UpdateProvider(value = UserSqlProvider.class,method = "update")
	void update(User user);
	/**
	 * 通过id查询用户
	 * @param id 用户id
	 * @return 用户
	 */
	@Select("select * from house_user where id=#{id}")
	User getById(Integer id);

	/**
	 * 通过账号查找用户
	 * @param account 用户账号
	 * @return 用户
	 */
	@Select("select * from house_user where account=#{account}")
	@Results({
			@Result(column = "id",property = "id",id = true),
			@Result(column = "account",property = "account"),
			@Result(column = "other_account",property = "otherAccount"),
			@Result(column = "password",property = "password"),
			@Result(column = "head_photo",property = "headPhoto"),
			@Result(column = "telephone",property = "telephone"),
			@Result(column = "regist_time",property = "registTime"),
			@Result(column = "user_sign",property = "userSign"),
			@Result(column = "status",property = "status"),
			@Result(column = "id", property = "posts", many = @Many(select = "com.woniuxy.house.mapper.PostMapper.getByUserId")),
			@Result(column = "id", property = "sources", many = @Many(select = "com.woniuxy.house.mapper.SourceMapper.getByUserId"))

	})
	User getByAccount(String account);

	/**
	 * 根据账号和密码查找用户（用于登录）
	 */
	@Select("select * from house_user where account=#{account} and password=#{password}")
	@Results({
			@Result(column = "id",property = "id",id = true),
			@Result(column = "account",property = "account"),
			@Result(column = "other_account",property = "otherAccount"),
			@Result(column = "password",property = "password"),
			@Result(column = "head_photo",property = "headPhoto"),
			@Result(column = "telephone",property = "telephone"),
			@Result(column = "regist_time",property = "registTime"),
			@Result(column = "user_sign",property = "userSign"),
			@Result(column = "status",property = "status"),
			@Result(column = "id", property = "posts", many = @Many(select = "com.woniuxy.house.mapper.PostMapper.getByUserId")),
			@Result(column = "id", property = "sources", many = @Many(select = "com.woniuxy.house.mapper.SourceMapper.getByUserId"))

	})
	User getUser(@Param("account")String account,@Param("password")String password);
	/**
	 * 无条件查询所有用户
	 * @return 用户集合
	 */
	@Select("select * from house_user")
	List<User> getUserList();

	/**
	 * 通过用户字段是否为空多条件查询用户
	 * @param user
	 * @return
	 */
	@SelectProvider(value = UserSqlProvider.class,method = "getUserListByCondition")
	List<User> getUserListByCondition(User user);


	/**
	 * 查出所有用户
	 */
	@Select("select* from house_user" )
	@Results({
			@Result(column = "id",property = "id",id = true),
			@Result(column = "account",property = "account"),
			@Result(column = "other_account",property = "otherAccount"),
			@Result(column = "password",property = "password"),
			@Result(column = "head_photo",property = "headPhoto"),
			@Result(column = "telephone",property = "telephone"),
			@Result(column = "regist_time",property = "registTime"),
			@Result(column = "user_sign",property = "userSign"),
			@Result(column = "status",property = "status"),
			@Result(column = "id", property = "posts", many = @Many(select = "com.woniuxy.house.mapper.PostMapper.getByUserId")),
			@Result(column = "id", property = "sources", many = @Many(select = "com.woniuxy.house.mapper.SourceMapper.getByUserId"))

	})
	List<User> getAll();
}
